function HandleProduces(gui, item, options) {
  this._gui = gui;
  this._item = item;
  this._options = options;
  
  this._query = (options && options.query) ? options.query : null;
  this._operator = (options && options.operator) ? options.operator : null;
  this._now = (options && options.now) ? options.now : null;
  this._menu = (options && options.menu) ? options.menu : null;
  
  this._nopage = (options && options.nopage) ? options.nopage : null;
  this._multiple = (options && options.multiple) ? options.multiple : null;
  this._smryGui = (options && options.smryGui) ? options.smryGui : null;
  this._smryArgs = (options && options.smryArgs) ? options.smryArgs : null;
  this._noExist = (options && options.noExist) ? options.noExist : false;
  
  this._createElements();
};

HandleProduces.prototype._createElements = function() {
  this._loadData();
};

HandleProduces.prototype._verifyData = function() {
  if (this._produces && this._events) {
    this._updateElements();
  }
};

HandleProduces.prototype._loadData = function() {
  this._produces = null;
  this._events = null;
  this._retrieveProduces();
  this._retrieveEvents();
};

HandleProduces.prototype._retrieveProduces = function(page, condition, pos) {
  var _self = this;
  condition = (condition) ? condition.replace(this._query + ' AND ', '') : '';
  var joins = '&selects=1' + '&purchaseitems=0' + '&preconusers=0' + '&events=0' + '&tracks=0' + '&produces=0' + '&notes=1';
  var args = '&c=(' + this._query  + ')' + ((condition) ? (' AND (' + condition + ')') : '') + '&p=' + ((page) ? page : 1) + '&s=' + ((this._nopage) ? '9999' : DlmanageUtils.number) + '&o=d.deadline' + '&q=ASC' + joins;
  new RequestUtils()._mysql('produce', args, function(result, params) { _self._produces = result.data;
                                                                         _self._parameters = result;
                                                                         _self._verifyData.call(_self);
                                                                       }, { php: 'customphp/produceread.php', pos: pos });
};

HandleProduces.prototype._retrieveEvents = function() {
  var _self = this;
  if (this._noExist || this._multiple) {
    this._events = [];
  } else {
    var args = '&operator=' + JSON.stringify(this._operator);
    new RequestUtils()._mysql('produceevent', args, function(result, params) { _self._events = result.data;
                                                                               _self._verifyData.call(_self);
                                                                             }, { php: 'customphp/mysqlcustom.php'});
  }
};

HandleProduces.prototype._updateElements = function() {
  var _self = this;
  DOMUtils.removeChildElements(this._gui.pages);
  DOMUtils.removeChildElements(this._gui.tasks);
  
  if (this._smryGui && this._smryArgs) {
    new HandleSmry(this._smryGui, null, { args: this._smryArgs });
  }
  
  var gui = GuiSearchTask.instance(this._gui.customerNo, this._gui.task, this._gui.search);
  new HandleSearchTask(gui, this._item, { callbackFunc: function(condition) { _self._retrieveProduces.call(_self, 1, condition); }, query: '(' + this._query + ')' });
  
  if (!this._nopage) {
    Pagination.makePagedResults(this._gui.pages, this._parameters.page, this._parameters.total, this._parameters.size, function(page, condition) { _self._retrieveProduces.call(_self, page, _self._parameters.condition); }, this, document);
  }
  
  if (this._multiple) {
    this._mailing = [];
    var branch, template, menu;
    branch = (this._produces.length > 0) ? this._produces[0].branch : '';
	menu = { a: 1, b: 2, c: 1};
    if (branch == '{{$smarty.const.NEWYORK|escape:javascript}}') {
      template = 'pageb2nybranchworks';
	  menu = { a: 1, b: 2, c: 1};
    } else if (branch == '{{$smarty.const.SYDNEY|escape:javascript}}') {
      template = 'pageb2sybranchworks';
	  menu = { a: 1, b: 2, c: 2};
    } else if (branch == '{{$smarty.const.AUCKLAND|escape:javascript}}') {
      template = 'pageb2akbranchworks';
	  menu = { a: 1, b: 2, c: 4};
    } else if (branch == '{{$smarty.const.MELBOURNE|escape:javascript}}') {
      template = 'pageb2mbbranchworks';
	  menu = { a: 1, b: 2, c: 3};
    } else if (branch == '{{$smarty.const.XTRAORDINARY|escape:javascript}}') {
      template = 'pageb2xtbranchworks';
	  menu = { a: 1, b: 2, c: 5};
    } else if (branch == '{{$smarty.const.BRISBANE|escape:javascript}}') {
      template = 'pageb2bnbranchworks';
	  menu = { a: 1, b: 2, c: 6};
    }
    this._gui.mail.onclick = function() { if (_self._mailing.length > 0) {
                                            location.href = '?p=pagefactory&t=pagemailinformation&m=' + JSON.stringify(menu) + '&opts=' + JSON.stringify({ p: DlmanageUtils.identifiers(_self._mailing), b: branch, t: template });
                                          }
                                        };
    new TableProducesMailing(this._gui.tasks, this._produces, { user: this._operator, date: this._now, produces: this._mailing, hidePrice: true });
  } else {
    new TableProduces(this._gui.tasks, this._produces, { user: this._operator, date: this._now, callbackFunc: function(pos) { _self._retrieveProduces.call(_self, 1, _self._parameters.condition, pos); _self._retrieveEvents();}, hidePrice: true, events: this._events, menu: this._menu, updated: this._gui.updated, noExist: this._noExist });
  }
};